package com.test.mooan.labama.utils_boom;

import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Cameroon;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Cote;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Ghana;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Kenya;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Senegal;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Tanzania;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Uganda;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_CONTENT_FILTERS_Zambia;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Cameroon;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Cote;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Ghana;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Kenya;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Senegal;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Tanzania;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Uganda;
import static com.test.mooan.labama.constants_boom.Constant_boom.SMS_MOBILE_FILTERS_Zambia;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_Cameroon;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_CoteDIvoire;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_Ghana;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_Kenya;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_Senegal;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_Tanzania;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_Uganda;
import static com.test.mooan.labama.constants_boom.Constant_boom.Timezone_Zambia;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.util.TimeZone;


public class Stringutil_boom {

    public static String getSafeString(String str){
        if (TextUtils.isEmpty(str)) return "";
        return str.trim();
    }

    //获取Url后缀
    public static String getUrlSuffix(String url){
        if (TextUtils.isEmpty(url)) return "";
        String[] strs = url.split("/");
        if (strs.length<5) return "/"+strs[strs.length-1];
        StringBuilder result = new StringBuilder();
        for (int i = 4;i<strs.length;i++){
            result.append("/").append(strs[i]);
        }
        return result.toString();
    }

    //金额显示格式化
    public static String amountOfMoneyFormat(String numStr){
        StringBuilder result=new StringBuilder();
        if (numStr.indexOf('.')!=-1){
            String[] strings = numStr.split("\\.");
            result.append(".").append(strings[1]);
            numStr = strings[0];
        }
        for (int i=numStr.length()-1,count=0;i>=0;i--){
            char c = numStr.charAt(i);
            if (c>='0'&&c<='9'){
                result.insert(0,c);
                count++;
            }
            if (count%3==0&&i>0){
                result.insert(0,',');
            }
        }
        return result.toString();
    }

    //银行账户显示后四位
    public static String bankAccountHideFront(String bankAccount){
        if (TextUtils.isEmpty(bankAccount)) return "";
        StringBuilder result = new StringBuilder();
        int length = bankAccount.length();
        if (length<=4) return bankAccount;
        for (int i = 1;i<=4;i++){
            result.insert(0,bankAccount.charAt(length-i));
        }
        result.insert(0,"*****");
        return result.toString();
    }

    //银行账户隐藏中间部分
    public static String bankAccountHideMiddle(String bankAccount){
        if (TextUtils.isEmpty(bankAccount)) return "";
        StringBuilder result = new StringBuilder();
        int length = bankAccount.length();
        for (int i = 1;i<=length;i++){
            if (i<=4||length-i<3) result.insert(0,bankAccount.charAt(length-i));
            else result.insert(0,"*");
        }
        return result.toString();
    }

    public static int safeParseInt(String str){
        str = str.trim();
        if (TextUtils.isEmpty(str)) return 0;
        StringBuilder safeStr = new StringBuilder();
        for (int i = 0;i<str.length();i++){
            char c = str.charAt(i);
            if (c>='0'&&c<='9') safeStr.append(c);
            else if (c == '.') return Integer.parseInt(safeStr.toString());
        }
        return Integer.parseInt(safeStr.toString());
    }

    public static double safeParseDouble(String str){
        try {
            if (TextUtils.isEmpty(str)) return 0;
            str = str.trim();
            return Double.parseDouble(str);
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        return 0;
    }

    public static String parseLimit(String limit){
        String[] strings = limit.split("-");
        return strings[strings.length-1];
    }


    public static String clearFrontZero(String str){
        if (TextUtils.isEmpty(str)) return "0";
        else if (!str.startsWith("0")) return str;
        int startIndex = 0;
        int endIndex = str.length();
        while (startIndex<endIndex&&str.charAt(startIndex)=='0') startIndex++;
        return str.substring(startIndex,endIndex);
    }

    //A.如果有加号开头，
    //去掉所有空格与-后，如果是以+255开头：去掉+255，其它：去掉+
    //
    //B.如果非加号开头，
    //去掉所有空格与-
//    public static String mobileFormat(String mobileStr){
//        mobileStr = mobileStr.replaceAll(" ","");
//        mobileStr = mobileStr.replaceAll("-","");
//        if (mobileStr.startsWith("+225")) mobileStr=mobileStr.replaceAll("\\+225","");
//        else if (mobileStr.startsWith("+")) mobileStr=mobileStr.replaceAll("\\+","");
//        return mobileStr;
//    }

    public static String processTitleFormat(String processName){
        if (TextUtils.isEmpty(processName)) return "";

        processName = processName.replaceAll("_"," ");
        processName = processName.substring(0,1).toUpperCase() + processName.substring(1);

        return processName;
    }

    public static String getDateDayFormat(int day){
        if (day%10==1) return day+"st";
        if (day%10==2) return day+"nd";
        return day+"th";
    }

    @SuppressLint("DefaultLocale")
    public static String formatDoubleString(double d){
        return String.format("%.2f",d);
    }


    public static boolean smsMobileFiltration(String mobile){

        String tzId = TimeZone.getDefault().getID();

        if (tzId == null) return filtration(mobile, new String[]{""});

        switch (tzId){
            case Timezone_Cameroon: return filtration(mobile, SMS_MOBILE_FILTERS_Cameroon);
            case Timezone_Ghana: return filtration(mobile, SMS_MOBILE_FILTERS_Ghana);
            case Timezone_CoteDIvoire: return filtration(mobile, SMS_MOBILE_FILTERS_Cote);
            case Timezone_Uganda: return filtration(mobile, SMS_MOBILE_FILTERS_Uganda);
            case Timezone_Zambia: return filtration(mobile, SMS_MOBILE_FILTERS_Zambia);
            case Timezone_Senegal: return filtration(mobile, SMS_MOBILE_FILTERS_Senegal);
            case Timezone_Tanzania: return filtration(mobile, SMS_MOBILE_FILTERS_Tanzania);
            case Timezone_Kenya: return filtration(mobile, SMS_MOBILE_FILTERS_Kenya);
        }

        return filtration(mobile, new String[]{""});
    }

    public static boolean smsContentFiltration(String content){

        String tzId = TimeZone.getDefault().getID();

        if (tzId == null) return filtration(content, new String[]{""});

        switch (tzId){
            case Timezone_Cameroon: return filtration(content, SMS_CONTENT_FILTERS_Cameroon);
            case Timezone_Ghana: return filtration(content, SMS_CONTENT_FILTERS_Ghana);
            case Timezone_CoteDIvoire: return filtration(content, SMS_CONTENT_FILTERS_Cote);
            case Timezone_Uganda: return filtration(content, SMS_CONTENT_FILTERS_Uganda);
            case Timezone_Zambia: return filtration(content, SMS_CONTENT_FILTERS_Zambia);
            case Timezone_Senegal: return filtration(content, SMS_CONTENT_FILTERS_Senegal);
            case Timezone_Tanzania: return filtration(content, SMS_CONTENT_FILTERS_Tanzania);
            case Timezone_Kenya: return filtration(content, SMS_CONTENT_FILTERS_Kenya);
        }

        return filtration(content, new String[]{""});
    }


    private static boolean filtration(String content, String[] filters){
        if (TextUtils.isEmpty(content)) return false;
        for (String filter:filters){
            if (content.toLowerCase().contains(filter.toLowerCase()))
                return true;
        }
        return false;
    }

}
